package top.xzhanyao.scaffold.tool.code.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * Mybatis-plus 代码生成器
 * 各项配置可以查看源码
 */
public class MysqlGeneratorEntity {

    @Test
    public void generateCode() {
        generateByTables("pin_card_info", "acs_authorization_info", "access_control_info",
                "building_info", "suite_info", "patrol_record", "fileupload_notice", "upload_file_info",
                "acs_records_info", "malfunction_record", "alarm_info", "street_user", "access_manufacturer",
                "police_district", "domestic_person_info", "oversea_person_info", "reside_record");
    }

    private void generateByTables(String... tableNames) {
        String dbUrl = "jdbc:mysql://192.168.1.221:3306/db_acdp";
        String packageName = "entities";

        String author = "zhanyao";
        String outputDir = "E:\\codeGen";
        String username = "root";
        String password = "pass@123";

        // 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setAuthor(author);
        config.setOutputDir(outputDir);
        config.setFileOverride(true);
        config.setServiceName("%sService");// 默认: I%sService
        config.setEntityName("%s");

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setUrl(dbUrl);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude(tableNames);// 修改替换成你需要的表名，多个表名传数组
        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(packageName);
        packageConfig.setController("controller");
        packageConfig.setXml("mapper");

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                // 模版引擎默认是 Veloctiy，选择 freemarker 需要添加指定模版引擎
                //.setTemplate(new FreemarkerTemplateEngine())
                .execute();
    }

}
